package com.jrtc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.PrescriptionLabelOne;
import com.jrtc.base.entity.bo.PrescriptionLabelThree;
import com.jrtc.base.entity.bo.PrescriptionLabelTwo;
import com.jrtc.dao.PrescriptionLabelOneDao;
import com.jrtc.dao.PrescriptionLabelThreeDao;
import com.jrtc.dao.PrescriptionLabelTwoDao;
import com.jrtc.service.PrescriptionLabelOneService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 运动处方标签一级分类(PrescriptionLabelOne)表服务实现类
 *
 * @author makejava
 * @since 2022-09-08 13:35:33
 */
@Service("prescriptionLabelOneService")
@Transactional
public class PrescriptionLabelOneServiceImpl implements PrescriptionLabelOneService
{
    @Resource
    private PrescriptionLabelOneDao prescriptionLabelOneDao;
    @Resource
    private PrescriptionLabelTwoDao prescriptionLabelTwoDao;
    @Resource
    private PrescriptionLabelThreeDao prescriptionLabelThreeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PrescriptionLabelOne queryById(Integer id) {
        return this.prescriptionLabelOneDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<PrescriptionLabelOne> queryAllByLimit(int offset, int limit) {
        return this.prescriptionLabelOneDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<PrescriptionLabelOne> queryAllLabel(int prescriptionId)
    {
        PrescriptionLabelOne tmpLabel = new PrescriptionLabelOne();
        tmpLabel.setExercisePrescriptionId(prescriptionId);
        List<PrescriptionLabelOne> lstLabel = this.prescriptionLabelOneDao.queryAll(tmpLabel);
        for (PrescriptionLabelOne prescriptionLabelOne : lstLabel)
        {
            List<PrescriptionLabelTwo> labelTwos = prescriptionLabelTwoDao.queryByParentId(prescriptionLabelOne.getId());
            if (labelTwos.size() > 0)
            {
                for (PrescriptionLabelTwo prescriptionLabelTwo : labelTwos)
                {
                    List<PrescriptionLabelThree> labelThrees = prescriptionLabelThreeDao.queryByParentId(prescriptionLabelTwo.getId());
                    if (labelThrees.size() > 0)
                    {
                        prescriptionLabelTwo.getLabelThrees().addAll(labelThrees);
                    }
                }
                prescriptionLabelOne.getLabelTwos().addAll(labelTwos);
            }
        }
        return lstLabel;
    }

    @Override
    public IPage<PrescriptionLabelOne> queryPrescriptionLabelOneList(PrescriptionLabelOne prescriptionLabelOne)
    {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<PrescriptionLabelOne> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(prescriptionLabelOne.getPageNo(), prescriptionLabelOne.getPageSize());
        IPage<PrescriptionLabelOne> prescriptionLabelOnePage = this.prescriptionLabelOneDao.queryPrescriptionLabelOneList(page, prescriptionLabelOne);
        return prescriptionLabelOnePage;
    }

    @Override
    public boolean insertPrescriptionLabel(List<PrescriptionLabelOne> lstLabel)
    {
        try
        {
            for (PrescriptionLabelOne prescriptionLabelOne : lstLabel)
            {
                prescriptionLabelOneDao.insert(prescriptionLabelOne);
                if (prescriptionLabelOne.getLabelTwos() != null && prescriptionLabelOne.getLabelTwos().size() > 0)
                {
                    for (PrescriptionLabelTwo prescriptionLabelTwo : prescriptionLabelOne.getLabelTwos())
                    {
                        prescriptionLabelTwoDao.insert(prescriptionLabelTwo);
                        if (prescriptionLabelTwo.getLabelThrees() != null && prescriptionLabelTwo.getLabelThrees().size() > 0)
                        {
                            for (PrescriptionLabelThree prescriptionLabelThree : prescriptionLabelTwo.getLabelThrees())
                            {
                                prescriptionLabelThreeDao.insert(prescriptionLabelThree);
                            }
                        }
                    }
                }
            }
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    /**
     * 新增数据
     *
     * @param prescriptionLabelOne 实例对象
     * @return 实例对象
     */
    @Override
    public PrescriptionLabelOne insert(PrescriptionLabelOne prescriptionLabelOne) {
        this.prescriptionLabelOneDao.insert(prescriptionLabelOne);
        return prescriptionLabelOne;
    }

    /**
     * 修改数据
     *
     * @param prescriptionLabelOne 实例对象
     * @return 实例对象
     */
    @Override
    public PrescriptionLabelOne update(PrescriptionLabelOne prescriptionLabelOne) {
        this.prescriptionLabelOneDao.update(prescriptionLabelOne);
        return this.queryById(prescriptionLabelOne.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.prescriptionLabelOneDao.deleteById(id) > 0;
    }
}